package com.hcrazy.manchester.app.services;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hcrazy.manchester.app.jdbc.UserGroupDAO;
import com.hcrazy.manchester.app.pojo.UserGroup;

public class UserGroupService {

	private UserGroupDAO userGroupDAO;
	
	
	public UserGroupDAO getUserGroupDAO() {
		return userGroupDAO;
	}

	public void setUserGroupDAO(UserGroupDAO userGroupDAO) {
		this.userGroupDAO = userGroupDAO;
	}
	
	public List<UserGroup> getAllGroups() throws Exception{
		return userGroupDAO.listAllGroups();
	}
	
	public UserGroup getById(int groupId) throws Exception{
		return userGroupDAO.getById(groupId);
	}
	
	public void delUserGroup(int groupId) throws Exception{
		userGroupDAO.delUserGroup(groupId);
	}
	
	public void addUserGroup(String groupStr) throws Exception {
		if(null == groupStr || "".equals(groupStr))
			return;
		Gson gson = new Gson();
		Type type = new TypeToken<List<UserGroup>>(){}.getType();
		List<UserGroup> groups = gson.fromJson(groupStr,type);
		
		userGroupDAO.deleteAll();
		userGroupDAO.addUserGroup(groups);
	}
}
